package com.demo8.common.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo8.basic.constant.CacheConstants;
import com.demo8.basic.domain.CommonResult;
import com.demo8.basic.utils.PwdUtil;
import com.demo8.basic.utils.RedisCacheUtils;
import com.demo8.basic.utils.StringUtils;
import com.demo8.common.domain.LoginBodyVO;
import com.demo8.system.domain.entity.SysUser;
import com.demo8.system.mapper.SysUserMapper;
import com.demo8.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/system/saToken")
public class SaTokenController {

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private RedisCacheUtils redisCacheUtils;
    @Autowired
    private SysUserMapper sysUserMapper;


    @PostMapping("login")
    public CommonResult<?> doLogin(@RequestBody @Validated LoginBodyVO loginBody) {

        //验证码验证
        if (configService.selectCaptchaEnabled()) {

            // 获取redisKey
            Object cacheObject = redisCacheUtils.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + Convert.toStr(loginBody.getUuid()));
            if (Objects.isNull(cacheObject)) {
                return CommonResult.fail("验证码失效");
            }
            if (!StringUtils.equals(cacheObject.toString(), loginBody.getCode())) {
                return CommonResult.fail("验证码错误");
            }
            redisCacheUtils.deleteObject(CacheConstants.CAPTCHA_CODE_KEY + Convert.toStr(loginBody.getUuid()));
        }
        //生产登录令牌
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", loginBody.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        if (Objects.isNull(sysUser)) {
            return CommonResult.fail("用户不存在");
        }
        String password = sysUser.getPassword();
        if (!PwdUtil.matches(loginBody.getPassword(), password)) {
            return CommonResult.fail("用户密码不正确");
        }

        StpUtil.login(sysUser.getUserId());
        return CommonResult.ok(StpUtil.getTokenInfo());
    }
    @SaCheckLogin
    @PostMapping("test")
    public CommonResult<?> test(@RequestBody LoginBodyVO loginBody) {
        //生产登录令牌
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", loginBody.getUsername());
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        if (Objects.isNull(sysUser)) {
            return CommonResult.fail("用户不存在");
        }
        String password = sysUser.getPassword();
        return CommonResult.ok(PwdUtil.matches(loginBody.getPassword(), password));

    }
    @RequestMapping("isLogin")
    public CommonResult<?> isLogin() {
        return CommonResult.ok("是否登录：" + StpUtil.isLogin());
    }
    @RequestMapping("logout")
    public CommonResult<?> logout() {
        StpUtil.logout();
        return CommonResult.ok();
    }
}
